Conditions | 32 |
Total Lines | 127 |
Code Lines | 92 |
Lines | 127 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like codef_scrolltext.js ➔ scrolltext_horizontal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*------------------------------------------------------------------------------ |
||
47 | View Code Duplication | function scrolltext_horizontal(){ |
|
|
|||
48 | this.scroffset=0; |
||
49 | this.oldspeed=0; |
||
50 | this.speed=1; |
||
51 | this.font; |
||
52 | this.letters = new Object(); |
||
53 | this.scrtxt=" "; |
||
54 | this.pausetimer=0; |
||
55 | this.pausedelay=0; |
||
56 | |||
57 | this.init = function(dst, font,speed,sinparam,type){ |
||
58 | this.speed=speed; |
||
59 | this.dst=dst; |
||
60 | this.font=font; |
||
61 | this.fontw = this.font.tilew; |
||
62 | this.fonth = this.font.tileh; |
||
63 | this.fontstart = this.font.tilestart; |
||
64 | this.wide=Math.ceil(this.dst.canvas.width/this.fontw)+1; |
||
65 | for(i=0;i<=this.wide;i++){ |
||
66 | this.letters[i]=new ltrobj(Math.ceil((this.wide*this.fontw)+i*this.fontw),0,this.scrtxt.charCodeAt(this.scroffset)); |
||
67 | this.scroffset++; |
||
68 | } |
||
69 | if(typeof(sinparam)!='undefined') |
||
70 | this.sinparam=sinparam; |
||
71 | if(typeof(type)=='undefined') |
||
72 | this.type=0; |
||
73 | else |
||
74 | this.type=type; |
||
75 | } |
||
76 | |||
77 | this.draw = function(posy){ |
||
78 | var prov = 0; |
||
79 | var temp = new Array(); |
||
80 | var tmp=this.dst.contex.globalAlpha; |
||
81 | this.dst.contex.globalAlpha=1; |
||
82 | var oldvalue=new Array(); |
||
83 | var i; |
||
84 | if(typeof(this.sinparam)!='undefined'){ |
||
85 | for(var j=0;j<this.sinparam.length;j++){ |
||
86 | oldvalue[j]=this.sinparam[j].myvalue; |
||
87 | } |
||
88 | } |
||
89 | if(this.speed==0){ |
||
90 | this.pausetimer+=1; |
||
91 | if(this.pausetimer==60*this.pausedelay){ |
||
92 | this.speed=this.oldspeed; |
||
93 | } |
||
94 | } |
||
95 | var speed=this.speed; |
||
96 | for(i=0;i<=this.wide;i++){ |
||
97 | this.letters[i].posx-=speed; |
||
98 | if(this.letters[i].posx<=-this.fontw){ |
||
99 | if(this.scrtxt.charAt(this.scroffset) =="^"){ |
||
100 | if(this.scrtxt.charAt(this.scroffset+1) =="P"){ |
||
101 | this.pausedelay=this.scrtxt.charAt(this.scroffset+2); |
||
102 | this.pausetimer=0; |
||
103 | this.oldspeed=this.speed; |
||
104 | this.speed=0; |
||
105 | this.scroffset+=3; |
||
106 | } |
||
107 | else if(this.scrtxt.charAt(this.scroffset+1) =="S"){ |
||
108 | this.speed=this.scrtxt.charAt(this.scroffset+2); |
||
109 | this.scroffset+=3; |
||
110 | } |
||
111 | // |
||
112 | // ADDON by Robert Annett |
||
113 | // |
||
114 | else if(this.scrtxt.charAt(this.scroffset+1) =="C"){ |
||
115 | var end = this.scrtxt.indexOf(';', this.scroffset+2); |
||
116 | var functionName = this.scrtxt.substring(this.scroffset+2, end); |
||
117 | window[functionName](); |
||
118 | this.scroffset+=(end-this.scroffset)+1; |
||
119 | } |
||
120 | }else{ |
||
121 | this.letters[i].posx=this.wide*this.fontw+(this.letters[i].posx+this.fontw); |
||
122 | if(typeof(this.sinparam)!='undefined'){ |
||
123 | for(var j=0;j<this.sinparam.length;j++){ |
||
124 | oldvalue[j]+=this.sinparam[j].inc; |
||
125 | } |
||
126 | } |
||
127 | this.letters[i].ltr=this.scrtxt.charCodeAt(this.scroffset); |
||
128 | this.scroffset++; |
||
129 | if(this.scroffset> this.scrtxt.length-1) |
||
130 | this.scroffset=0; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | if(typeof(this.sinparam)!='undefined'){ |
||
135 | for(var j=0;j<this.sinparam.length;j++){ |
||
136 | this.sinparam[j].myvalue=oldvalue[j]; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | for(j=0;j<=this.wide;j++){ |
||
141 | temp[j]={indice:j, posx:this.letters[j].posx}; |
||
142 | } |
||
143 | temp.sort(sortPosx); |
||
144 | for(i=0;i<=this.wide;i++){ |
||
145 | if(typeof(this.sinparam)!='undefined'){ |
||
146 | prov = 0; |
||
147 | for(var j=0;j<this.sinparam.length;j++){ |
||
148 | if(this.type==0) |
||
149 | prov += Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp; |
||
150 | if(this.type==1) |
||
151 | prov += -Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
152 | if(this.type==2) |
||
153 | prov += Math.abs(Math.sin(this.sinparam[j].myvalue)*this.sinparam[j].amp); |
||
154 | |||
155 | } |
||
156 | } |
||
157 | this.font.drawTile(this.dst,this.letters[temp[i].indice].ltr-this.fontstart,this.letters[temp[i].indice].posx,prov+posy); |
||
158 | |||
159 | if(typeof(this.sinparam)!='undefined'){ |
||
160 | for(var j=0;j<this.sinparam.length;j++){ |
||
161 | this.sinparam[j].myvalue+=this.sinparam[j].inc; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | if(typeof(this.sinparam)!='undefined'){ |
||
166 | for(var j=0;j<this.sinparam.length;j++){ |
||
167 | this.sinparam[j].myvalue=oldvalue[j]+this.sinparam[j].offset; |
||
168 | } |
||
169 | } |
||
170 | this.dst.contex.globalAlpha=tmp; |
||
171 | } |
||
172 | return this; |
||
173 | } |
||
174 | |||
303 |